package leeon.bbsbrower.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import leeon.bbsbrower.BBSBrowerPlugin;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

/**
 * ͼ
 */
public class BrowerImages {
	private static ImageRegistry imageRegistry;
	private final static String ICONS_PATH = "icons/";

	public final static String ICONS_OBJ_SYSTRAY = "SYSTRAY_CONN";
	
	
	final static String PIC_PATH = "pic";
	//图片路径缓存,如果有key但是没有对象，表示正在下载，key没有，表示没有该图片
	private static HashMap<String, ImageDescriptor> map = new HashMap<String, ImageDescriptor>();
	
	/**
	 * 初始化静态区域 
	 */
	static {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
			declareImages();
		}
		
		try {
			File dir = BBSBrowerPlugin.getRealFile(PIC_PATH);
			if (dir.isDirectory() && dir.exists()) {
				File[] files = dir.listFiles();
				for (int i = 0; i < files.length; i ++) {
					if (files[i].isFile() && files[i].exists()) {
						map.put(files[i].getName(), BBSBrowerPlugin.getImageDescriptor(PIC_PATH+"/"+files[i].getName()));
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}

	/**
	 * 
	 */
	private final static void declareImages() {
		declareRegistryImage(ICONS_OBJ_SYSTRAY, ICONS_PATH + "systray.gif");
	}

	private final static void declareRegistryImage(String key, String path) {
		ImageDescriptor desc = BBSBrowerPlugin.getImageDescriptor(path);
		imageRegistry.put(key, desc);
	}

	/**
	 * ȡ��
	 * 
	 */
	public static Image getImage(String key) {
		return imageRegistry.get(key);
	}

	public static ImageDescriptor getImageDescriptor(String key) {
		return imageRegistry.getDescriptor(key);
	}
	
	
	/**
	 * pool中图片的状态，0 没有，1 有正在下载，2 已经可用, 3 坏链接
	 * @param key
	 * @return
	 */
	public static int getImageStatus(String key) {
		if (map.containsKey(key)) {
			Object o = map.get(key);
			if (o == null) {
				return 1;
			} else if (getImageDescriptor(ICONS_OBJ_SYSTRAY).equals(o)) {
				return 3;
			} else {
				return 2;
			}			
		} else {
			return 0;
		}
	}	
	
	/**
	 * 当pool中有的情况下，获取ImageDescriptor
	 * @param key
	 * @return
	 */
	public static ImageDescriptor getImageFromPool(String key) {
		return (ImageDescriptor)map.get(key);
	}
	
	/**
	 * 加入一个正在下载的信息
	 * @param key
	 */
	public static void addDownloadingImage(String key) {
		map.put(key, null);		
	}
	
	/**
	 * 加入一个准备好了的图片
	 * @param key
	 */
	public static void addStandbyImage(String key) {
		map.put(key, BBSBrowerPlugin.getImageDescriptor(PIC_PATH+"/"+key));
	}
	
	/**
	 * 加入一个坏链接图片
	 * @param key
	 */
	public static void addBadImage(String key) {
		map.put(key, getImageDescriptor(ICONS_OBJ_SYSTRAY));
	}
	
	public static void removeImage(String key) {
		map.remove(key);
	}
	/**
	 * 处理掉不能用的符号
	 * @param source
	 * @return
	 */
	public static String dealImageName(String source) {
		if (source != null)
			return source.replace('/', '_')
				.replace(':', '_')
				.replace('?', '_')
				.replace('*', '_')
				.replace('"', '_');
		else
			return null;
	}
	
	/**
	 * 查找一行中的图片url
	 */
	public static String findImageURL(String source) {
		int s = source.indexOf("http://");
		if (s == -1) return null;
		
		int e = source.indexOf(" ", s);
		if (e != -1) source = source.substring(s, e);		
		else source = source.substring(s);
		
		if (source.toLowerCase().endsWith(".jpg") ||
			source.toLowerCase().endsWith(".gif") ||
			source.toLowerCase().endsWith(".bmp")) return source;
		else return null;
	}
	

	
}